package cn.daliyTest.com.thread.processCommunication;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试进程间通信
 * volatile
 *
 * @author ：ljf
 * @date ：Created in 2021/6/24 10:50
 */
public class TestSyncByVolatile {

    static volatile boolean notice = false;

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 10; i++) {

                    list.add("abc");
                    System.out.println("线程A向list中添加一个元素，此时list中的元素个数为：" + list.size());
                    if (list.size() == 5) {
                        notice = true;
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {

                }

            }
        });

        Thread threadB = new Thread(() -> {
            while (true) {
                if (notice) {
                    System.out.println("threadB 开始工作");
                    break;
                }
            }
        });

        threadB.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadA.start();
    }
}
